import {
	defineConfig
} from 'vite';
import path, {
	resolve
} from 'path';
import uni from '@dcloudio/vite-plugin-uni';

// 自动导入composition api
import AutoImport from 'unplugin-auto-import/vite';
import {
	autoImportList,
	lodashList
} from './src/utils/autoImport';

const fs = require('fs');

export default defineConfig({
	plugins: [
		uni(),
		// 配置自动导入 vue相关函数, uni-app相关函数。ref, reactive，onLoad等
		AutoImport({
			// 自动导入
			imports: [
				'vue',
				'uni-app',
				'pinia',
				{
					// 使用的时候才会自动导入，检查js代码
					'@/utils/autoImport': autoImportList,
					lodash: lodashList,
					vue: ['h'],
				},
			],
			dts: '.auto-import.d.ts', // 生成 `auto-import.d.ts` 全局声明
		}),
	],
	resolve: {
		alias: {
			"@": resolve(__dirname, "src"),
			"@a": resolve(__dirname, "src/api"),
			"@u": resolve(__dirname, "src/utils"),
		},
	},
	server: {
		port: '5173',
		https: {
			cert: fs.readFileSync(path.join(__dirname, 'src/ssl/cert.crt')),
			key: fs.readFileSync(path.join(__dirname, 'src/ssl/cert.key'))
		},
		proxy: {
			'/metaworks': {
				target: 'https://117.191.103.69:38443',
				changeOrigin: true,
				secure: false,
				bypass(req, res, options) {
					const proxyURL = options.target + req.url;
					res.setHeader('x-req-proxyURL', proxyURL) // 将真实请求地址设置到响应头中
				},
			},
			'/artemis': {
				target: 'https://117.191.103.69:45003',
				changeOrigin: true,
				secure: false,
				bypass(req, res, options) {
					const proxyURL = options.target + req.url;
					res.setHeader('x-req-proxyURL', proxyURL) // 将真实请求地址设置到响应头中
				},
			},
			'/apiaccess': {
				target: 'https://117.191.103.69:38443',
				changeOrigin: true,
				secure: false,
			},
			'/prod-api': {
				target: 'https://117.191.103.69:39400',
				changeOrigin: true,
				bypass(req, res, options) {
					const proxyURL = options.target + req.url;
					res.setHeader('x-req-proxyURL', proxyURL) // 将真实请求地址设置到响应头中
				},
			},
		},
	}
});